package com.ruoyi.bizsys.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.AppUser;
import com.ruoyi.bizsys.mapper.AppUserMapper;
import com.ruoyi.bizsys.service.IAppUserService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
    import com.ruoyi.bizsys.mapper.AppsflyerChannelMapper;
import com.ruoyi.bizsys.domain.AppsflyerChannel;
import com.ruoyi.bizsys.service.IAppsflyerChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


/**
 * appsflyerChannelService业务层处理
 *
 * @author ygw
 * @date 2022-08-12
 */
@Service
public class AppsflyerChannelServiceImpl extends ServiceImpl<AppsflyerChannelMapper,AppsflyerChannel> implements IAppsflyerChannelService {
    /**
     * 查询appsflyerChannel
     *
     * @param id appsflyerChannelID
     * @return appsflyerChannel
     */
    @Override
    public AppsflyerChannel selectAppsflyerChannelById(Integer id) {
        return this.getById(id);
    }

    /**
     * 查询appsflyerChannel列表
     *
     * @param appsflyerChannel appsflyerChannel
     * @return appsflyerChannel
     */
    @Override
    public List<AppsflyerChannel> selectAppsflyerChannelList(AppsflyerChannel appsflyerChannel) {
        return this.list(new QueryWrapper<>(appsflyerChannel).last("order by id desc"));
    }

    /**
     * 新增appsflyerChannel
     *
     * @param appsflyerChannel appsflyerChannel
     * @return 结果
     */
            @Override
    public int insertAppsflyerChannel(AppsflyerChannel appsflyerChannel) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                appsflyerChannel.setCreateTime(DateUtils.getNowDate());
                                                return baseMapper.insert(appsflyerChannel);
    }

    /**
     * 修改appsflyerChannel
     *
     * @param appsflyerChannel appsflyerChannel
     * @return 结果
     */
            @Override
    public int updateAppsflyerChannel(AppsflyerChannel appsflyerChannel) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    appsflyerChannel.setUpdateTime(DateUtils.getNowDate());
                            return baseMapper.updateById(appsflyerChannel);
    }

    /**
     * 批量删除appsflyerChannel
     *
     * @param ids 需要删除的appsflyerChannelID
     * @return 结果
     */
            @Override
    public int deleteAppsflyerChannelByIds(Integer[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除appsflyerChannel信息
     *
     * @param id appsflyerChannelID
     * @return 结果
     */
    @Override
    public int deleteAppsflyerChannelById(Integer id) {
        return baseMapper.deleteById(id);
    }

    @SneakyThrows
    @Override
    public void insertChannel(JSONObject dataJson) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        AppsflyerChannel appsflyerChannel = new AppsflyerChannel();
        appsflyerChannel.setAfViewthroughLookback(dataJson.getString("af_viewthrough_lookback"));
        appsflyerChannel.setClickUrl(dataJson.getString("click_url"));
        appsflyerChannel.setCostCentsUsd(dataJson.getString("cost_cents_USD"));
        appsflyerChannel.setExternalAccountId(dataJson.getString("external_account_id"));
        appsflyerChannel.setGclid(dataJson.getString("gclid"));
        appsflyerChannel.setAfDp(dataJson.getString("af_dp"));
        appsflyerChannel.setAfKeywords(dataJson.getString("af_keywords"));
        appsflyerChannel.setAfPrt(dataJson.getString("af_prt"));
        appsflyerChannel.setAfReengagementWindow(dataJson.getString("af_reengagement_window"));
        appsflyerChannel.setAdEventId(dataJson.getString("ad_event_id"));
        appsflyerChannel.setAdgroupName(dataJson.getString("adgroup_name"));
        appsflyerChannel.setAfAd(dataJson.getString("af_ad"));
        appsflyerChannel.setAfAdId(dataJson.getString("af_ad_id"));
        appsflyerChannel.setAfAdType(dataJson.getString("af_ad_type"));
        appsflyerChannel.setAfAdset(dataJson.getString("af_adset"));
        appsflyerChannel.setAfAdsetId(dataJson.getString("af_adset_id"));
        appsflyerChannel.setAfCId(dataJson.getString("af_cId"));
        appsflyerChannel.setAfClickLookback(dataJson.getString("af_click_lookback"));
        appsflyerChannel.setAfCpi(dataJson.getString("af_cpi"));
        appsflyerChannel.setAddId(dataJson.getString("add_id"));
        appsflyerChannel.setAdgroup(dataJson.getString("adgroup"));
        appsflyerChannel.setAdgroupId(dataJson.getString("adgroup_id"));
        appsflyerChannel.setAdset(dataJson.getString("adset"));
        appsflyerChannel.setAdsetId(dataJson.getString("adset_id"));
        appsflyerChannel.setAfChannel(dataJson.getString("af_channel"));
        appsflyerChannel.setAfSiteid(dataJson.getString("af_siteid"));
        appsflyerChannel.setAfStatus(dataJson.getString("af_status"));
        appsflyerChannel.setAfSub1(dataJson.getString("af_sub1"));
        appsflyerChannel.setAfSub2(dataJson.getString("af_sub2"));
        appsflyerChannel.setAfSub3(dataJson.getString("af_sub3"));
        appsflyerChannel.setAfSub4(dataJson.getString("af_sub4"));
        appsflyerChannel.setAfSub5(dataJson.getString("af_sub5"));
        appsflyerChannel.setAgency(dataJson.getString("agency"));
        appsflyerChannel.setCampaign(dataJson.getString("campaign"));
        appsflyerChannel.setCampaignId(dataJson.getString("campaign_id"));
        appsflyerChannel.setHttpReferrer(dataJson.getString("http_referrer"));
        appsflyerChannel.setIsFb(dataJson.getString("is_fb"));
        appsflyerChannel.setIsFirstLaunch(dataJson.getString("is_first_launch"));
        appsflyerChannel.setIsMobileDataTermsSigned(dataJson.getString("is_mobile_data_terms_signed"));
        appsflyerChannel.setIsPaid(dataJson.getString("is_paid"));
        appsflyerChannel.setMatchType(dataJson.getString("match_type"));
        appsflyerChannel.setMediaSource(dataJson.getString("media_source"));
        appsflyerChannel.setRetargetingConversionType(dataJson.getString("retargeting_conversion_type"));
        appsflyerChannel.setAdvertising(dataJson.getString("advertising"));
        appsflyerChannel.setAfMessage(dataJson.getString("af_message"));
        appsflyerChannel.setIsIncentivized(dataJson.getString("is_incentivized"));
        appsflyerChannel.setEngmntSource(dataJson.getString("engmnt_source"));
        appsflyerChannel.setRedirectResponseData(dataJson.getString("redirect_response_data"));
        appsflyerChannel.setAfQr(dataJson.getString("af_qr"));
        appsflyerChannel.setIsRetargeting(dataJson.getString("is_retargeting"));
        appsflyerChannel.setIsUniversalLink(dataJson.getString("is_universal_link"));
        appsflyerChannel.setEspName(dataJson.getString("esp_name"));
        appsflyerChannel.setShortlink(dataJson.getString("shortlink"));
        appsflyerChannel.setIsBrandedLink(dataJson.getString("is_branded_link"));
        appsflyerChannel.setIscache(dataJson.getString("iscache"));
        appsflyerChannel.setLat(dataJson.getString("lat"));
        appsflyerChannel.setNetwork(dataJson.getString("network"));
        appsflyerChannel.setOrigCost(dataJson.getString("orig_cost"));
        appsflyerChannel.setVideoId(dataJson.getString("video_ud"));
        appsflyerChannel.setUserId(dataJson.getString("userId"));
        try {
            if(!StringUtils.isEmpty(dataJson.getString("install_time"))){
                appsflyerChannel.setInstallTime(simpleDateFormat.parse(dataJson.getString("install_time")));
            }
            if(!StringUtils.isEmpty(dataJson.getString("click_time"))){
                appsflyerChannel.setClickTime(simpleDateFormat.parse(dataJson.getString("click_time")));
            }
            if(!StringUtils.isEmpty(dataJson.getString("click_timestamp"))){
                appsflyerChannel.setClickTimestamp(simpleDateFormat.parse(dataJson.getString("click_timestamp")));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        appsflyerChannel.setCreateTime(new Date());
        appsflyerChannel.setUpdateTime(new Date());
        baseMapper.insert(appsflyerChannel);
    }

}
